package exams.list;
import java.util.*;

public class SortListSolution2 {

    public ListNode sortInList (ListNode head) {
        Integer.valueOf('1');
        // write code here
        if(head == null || head.next == null) {
            return head;
        }
        ArrayList<Integer> headArrayList = new ArrayList<>();
        headArrayList.add(head.val);
        while(head.next != null) {
            headArrayList.add(head.next.val);
            head = head.next;
        }
        headArrayList.sort((Integer o1, Integer o2) -> o2 - o1);
        ListNode newHead = new ListNode(-1);
        ListNode p = newHead;
        for(int v: headArrayList) {
            p.next = new ListNode(v);
            p = p.next;
        }
        return newHead.next;
    }

}
